package controlLayer;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import modelLayer.Customer;
import modelLayer.Discount;
import modelLayer.Item;
import modelLayer.QuantityDiscount;
import modelLayer.Sale;
import modelLayer.SaleContainer;
import modelLayer.SaleLine;
import modelLayer.Staff;

public class SaleController {
	public Sale makeSale(String username, int cprNo, Map<Integer,Integer> line){
		Date date = new Date();
		Sale sale = new Sale(0,date.toString(),addStaff(username));//not done yet
		//go through the keys of the map 
		Iterator itr = line.keySet().iterator();
		while(itr.hasNext()){ // while it still has keys to search for 
			int barcode = (int)itr.next();
			int quantity = line.get(barcode);
			//make a saleLine with the item and it's quantity
			SaleLine saleLine = addItemTo(barcode,quantity);
			//if created successfully, add it inside the sale;
			if(saleLine != null)
				sale.addSaleLine(saleLine);
				sale.setTotalPrice((sale.getTotalPrice()+saleLine.getPrice()));
		}
		//apply the discount to the sale 
		return sale;
	}

	public SaleLine addItemTo(int barcode, int quantity){
		ItemController itemCTR = new ItemController();
		Item item = itemCTR.findItem(barcode);
		double price = itemCTR.findItem(barcode).getPrice();
		/*
		 * Check if the quantity is not bigger than the quantity of the item
		 */
		if(itemCTR.checkQuantity(barcode, quantity)){
			//update the quantity inside the modelLayer
			item.setQuantity((item.getQuantity()-quantity));
			price = price*quantity; // set the price for SaleLine
			//create the saleLine with the parameters
			SaleLine saleLine = new SaleLine(barcode, price, quantity);
			return saleLine;
		}
		return null;
	}
	
	public Staff addStaff(String username){
		StaffController staffCTR = new StaffController();
		return staffCTR.findStaffByUserName(username);
	}
	
	public Customer addCustomer(int cprNo){
		CustomerController customerCTR = new CustomerController();
		return customerCTR.findCustomer(cprNo);
	}
	
	public double checkDiscount(Sale sale, int cprNo){
		double discount = 0;
		int totalQuantity = 0;
		ArrayList<SaleLine> saleLines = sale.getSaleLines();
		double totalPrice = sale.getTotalPrice();
		CustomerController customerCTR = new CustomerController();
		
		//get the total quantity
		for(SaleLine sl : saleLines){
			totalQuantity = sl.getQuantity();
		}
		Discount d = new QuantityDiscount();
		//add Quantity Discount;
		if(totalQuantity >= ((QuantityDiscount)d).getMinQuantity()){
			discount += d.getAmountInPercent();
		}
		//get the person Discount
		if(cprNo != 0){
			discount += customerCTR.getDiscount(cprNo);
			Customer c = customerCTR.findCustomer(cprNo);
			//add CraftsMan Discount;
			if(c.getIsCraftsman()){
				discount += d.getAmountInPercent();
			}
		}
		if(discount > 20){
			discount = 20;
		}
		return discount;
	}
	
	public double makePayment(Sale sale, double money){
		double totalPrice = sale.getTotalPrice();
		double change = 0 ;
		change = money - totalPrice;
		if(change >= 0)
			endSale(sale);
		return change;
	}
	
	public String endSale(Sale sale){
		SaleContainer saleContainer = SaleContainer.getInstance();
		saleContainer.addSale(sale);
		return "Successfully finish!";
	}
}
